package com.interauto.service;

import com.interauto.bean.AutoAssInfoBean;
import com.interauto.bean.AutoAssertInfoBean;
import com.interauto.bean.AutoResAllBean;
import com.interauto.bean.httpinfo.HttpRequestBean;
import com.interauto.bean.httpinfo.HttpResPonseBean;
import com.interauto.bean.autoenum.HttpEnum;
import com.interauto.bean.exception.AutoAssertException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.ReadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * 运行接口的
 */
@Component
public class AutoRunInterfaceService {
    private static final Logger logger= LoggerFactory.getLogger(AutoRunInterfaceService.class);
    @Autowired
    private HttpAutoClientService httpAutoClientService;
    @Autowired
    private AutoAssertService autoAssertService;

    public HttpResPonseBean runifcase(HttpRequestBean httpRequestBean){
        if(httpRequestBean == null){
            return null;
        }
        logger.info("runifcase info is --->"+httpRequestBean.toString());
        HttpEnum.HttpMeth httpmeth=httpRequestBean.getHttpmeth();
        HttpResPonseBean httpResPonseBean=null;
        switch (httpmeth){
            case GET:
                httpResPonseBean=httpAutoClientService.autoget(httpRequestBean);
                break;
            case POST:
                httpResPonseBean=httpAutoClientService.autopost(httpRequestBean);
                break;
            default:
                break;
        }
        if(httpResPonseBean == null){
            logger.info("httpResPonseBean is null");
            return null;
        }
        logger.info("httpResPonseBean is ---->"+httpResPonseBean);
        return httpResPonseBean;
    }

    public AutoAssertInfoBean runcheckinfo(HttpResPonseBean httpResPonseBean, ArrayList<AutoAssInfoBean> autoAssInfoBeans){
        if(httpResPonseBean == null){
            return new AutoAssertInfoBean(3,"响应存在异常");
        }

        if(autoAssInfoBeans==null||autoAssInfoBeans.isEmpty()){
            return new AutoAssertInfoBean(3,"校验体参数存在异常");
        }
        if(httpResPonseBean.getRescode()!=200){  //先直接校验httpcode==200
            return new AutoAssertInfoBean(1,"http响应状态不是200");
        }
        AutoResAllBean autoResAllBean;
        try{
            autoResAllBean=getHttpAutoResAllbean(httpResPonseBean);
        }
        catch (AutoAssertException e){
            System.out.println("--->>>>>>e 1haha:"+e.getMessage());
            return new AutoAssertInfoBean(2,e.getMessage());
        }
        Class<?> AutoAssertClObj=autoAssertService.getClass();
        for(AutoAssInfoBean autoAssInfo:autoAssInfoBeans){
            autoResAllBean.setCheckinfo(autoAssInfo.getCheckinfo());
            autoResAllBean.setExpectinfo(autoAssInfo.getExpectinfo());
            String funname=autoAssInfo.getAutoExpectEnum().getCheckname();
            try{
                Method AssMethod = AutoAssertClObj.getMethod(funname,AutoResAllBean.class);  //通过反射去驱动执行方法
                AssMethod.invoke(autoAssertService,autoResAllBean);
            }
            catch (Exception e){
                if(e instanceof InvocationTargetException){
                    InvocationTargetException ie=(InvocationTargetException)e;
                    return new AutoAssertInfoBean(2,ie.getTargetException().getMessage());
                }
                e.printStackTrace();
                return new AutoAssertInfoBean(1,e.getMessage());
            }
        }
        return new AutoAssertInfoBean(0,"ok");
    }

    private AutoResAllBean getHttpAutoResAllbean(HttpResPonseBean httpResPonseBean) throws AutoAssertException{
        AutoResAllBean autoResAllBean=new AutoResAllBean();
        autoResAllBean.setType(1);
        autoResAllBean.setHttpResPonseBean(httpResPonseBean);
        Integer restype=httpResPonseBean.getRestype();
        autoResAllBean.setRestype(restype);  //这里有危险，必须保证俩面是对齐的
        if(restype == 1){
            try{
                ReadContext ctx=JsonPath.parse(httpResPonseBean.getResinfo());
                autoResAllBean.setObjres(ctx);
            }
            catch (Exception e){
                throw new AutoAssertException("处理http响应结果体转换json系统异常："+e.getMessage());
            }
        }
        else if(restype==3){
            autoResAllBean.setObjres(httpResPonseBean.getResinfo());
        }
        return autoResAllBean;
    }

    /**
     * 当为cookie和header校验时，从里面取值。 暂时不弄这个了
     * @param httpResPonseBean
     * @param checkinfo
     */
    public void getheadercheck(HttpResPonseBean httpResPonseBean,String checkinfo) throws AutoAssertException {
        if(!checkinfo.startsWith("cookie") && checkinfo.startsWith("header")){
            logger.info("check header is fail1;checkinfo is--->"+checkinfo);
            return;
        }
        String[] checkarrays=checkinfo.split("\\.");
        if(checkarrays.length<2){
            logger.info("check header is fail2;checkinfo is--->"+checkinfo);
            return;
        }
        String checkvalue=checkarrays[1];
        if(checkinfo.startsWith("cookie")){
            if(httpResPonseBean.getRescookie() == null || httpResPonseBean.getRescookie().isEmpty()){
                logger.info("check cookie is fail; cookie is null");
                return;
            }
            String cookievalue=httpResPonseBean.getRescookie().get(checkvalue);
            return;
        }
        else {
            if(httpResPonseBean.getResheader() == null || httpResPonseBean.getResheader().isEmpty()){
                logger.info("check header is fail; header is null");
                return;
            }
            String headervalue=httpResPonseBean.getResheader().get(checkvalue);
        }
    }

    public static void main(String[] args) {

    }
}
